package org.yanse.utils;

public class PolygonUtil {
    public static double[] generatePolygonCoordinates(int n, int size) {
        return generatePolygonCoordinates(n, size, 0, 0, 0);
    }


    /**
     *
     * @param n n边
     * @param size 边长
     * @param pointX 中心点x坐标
     * @param pointY 中心点y坐标
     * @param angle 与0度逆时针的夹角
     * @return n边形的坐标
     */
    public static double[] generatePolygonCoordinates(int n, int size, double pointX, double pointY, double angle) {
        if (n < 3 || size < 0) {
            return new double[0];
        }

        double[] nPolygonArray = new double[n * 2];
        // 每次旋转的角度
        double rAngle = 360d / n;
        // 半径
        double r = size / 2d / Math.sin(Math.toRadians(rAngle / 2d));
        for (int i = 0; i < nPolygonArray.length; i += 2) {
            if (i != 0) {
                angle += rAngle;
            }
            // 弧度
            double rotate = Math.toRadians(angle);
            double x = pointX + Math.cos(rotate) * r;
            double y = pointY + Math.sin(rotate) * r;
            nPolygonArray[i] = x;
            nPolygonArray[i + 1] = y;
        }

        return nPolygonArray;
    }
}
